import 'package:flutter/material.dart';

import '../../utils/util.dart';
import 'controller.dart';
import 'model.dart';

class SelectionContentView extends StatelessWidget {
  const SelectionContentView({
    Key? key,
    required this.md,
    required this.controller,
  }) : super(key: key);
  final SelectionModel md;
  final SelectionController controller;
  @override
  Widget build(BuildContext context) {
    controller.mod = md;
    return GetBuilder<SelectionController>(
        init: controller,
        builder: (_) {
          return _buildContentView(
            md,
          );
        });
  }

  Widget _buildContentView(SelectionModel mod) {
    SelectionModel? selMod;
    if (mod.list != null) {
      for (var e in mod.list!) {
        if (e.isShow && e.title == null) {
          selMod = e;
        }
      }
    }

    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Expanded(
          flex: 2,
          child: _buildItemsView(
            mod,
            backgroundColor: ColorsUtil.bg,
          ),
        ),
        selMod == null
            ? const SizedBox()
            : Expanded(
                flex: 5,
                child: _buildItemsView(selMod,
                    parent: mod, backgroundColor: Colors.white),
              ),
      ],
    );
  }

  Widget _buildItemsView(
    SelectionModel mod, {
    SelectionModel? parent,
    Color? backgroundColor,
  }) {
    return Container(
      color: backgroundColor,
      alignment: Alignment.topLeft,
      child: _buildStyleView(
        mod: mod,
        children: mod.list
            ?.asMap()
            .map(
              (key, value) {
                return MapEntry(
                  key,
                  InkWell(
                    onTap: () {
                      controller.actionItem(md: value, parent: mod);
                    },
                    child: _buildItemView(
                      md: value,
                      parent: parent,
                      backgroundColor:
                          value.isShow ? Colors.white : backgroundColor,
                    ),
                  ),
                );
              },
            )
            .values
            .toList(),
      ),
    );
  }

  Widget _buildStyleView({
    required SelectionModel mod,
    required List<Widget>? children,
  }) {
    if (children == null) {
      return const SizedBox();
    }

    var style = mod.style;

    if (style == SelectionStyle.row) {
      return Row(
        mainAxisSize: MainAxisSize.min,
        children: children
            .map(
              (e) => Flexible(child: e),
            )
            .toList(),
      );
    } else if (mod.style == SelectionStyle.wrap) {
      double spacing = mod.list?.first.itemStyle == SelectionItemStyle.tag
          ? (mod.rowCount > 1 ? 10.r : 0)
          : 0;
      var w = (Get.width - 24.r - (mod.rowCount - 1) * spacing) / mod.rowCount;
      return Padding(
        padding: EdgeInsets.symmetric(horizontal: 12.r),
        child: Wrap(
          runSpacing: spacing,
          spacing: spacing,
          children: children
              .map(
                (e) => SizedBox(
                  width: w,
                  child: e,
                ),
              )
              .toList(),
        ),
      );
    }
    return Container(
      color: ColorsUtil.bg,
      child: SingleChildScrollView(
        physics: const ClampingScrollPhysics(),
        child: Column(
          children: children,
        ),
      ),
    );
  }

  Widget _buildItemView({
    required SelectionModel md,
    SelectionModel? parent,
    Color? backgroundColor,
  }) {
    if (!isNilStr(md.title) && parent == null) {
      return _buildItemTitle(md: md);
    }
    if (md.child != null) {
      return md.child!;
    }
    if (md.itemStyle == SelectionItemStyle.none) {
      return _buildItemNone(md: md, backgroundColor: backgroundColor);
    } else if (md.itemStyle == SelectionItemStyle.radio) {
      return _buildItemRadio(md: md, backgroundColor: Colors.white);
    } else if (md.itemStyle == SelectionItemStyle.tag) {
      return _buildItemTag(md: md);
    } else if (md.itemStyle == SelectionItemStyle.date) {
      return _buildItemDate(md: md);
    } else if (md.itemStyle == SelectionItemStyle.input) {
      return _buildItemInput(md: md);
    } else {
      return _buildItemCheckBox(md: md, backgroundColor: backgroundColor);
    }
  }

  Widget _buildItemNone({
    required SelectionModel md,
    Color? backgroundColor,
  }) {
    return Container(
      height: 50.r,
      color: backgroundColor ?? (md.isShow ? Colors.white : ColorsUtil.bg),
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.symmetric(horizontal: 10.r),
      child: Text(
        md.text,
        style: TextStyle(
          color: ColorsUtil.title,
          fontSize: md.isSelected ? 14.sp : 12.sp,
          fontWeight: md.isSelected ? FontWeight.bold : FontWeight.normal,
        ),
      ),
    );
  }

  Widget _buildItemTag({
    required SelectionModel md,
  }) {
    return Container(
      height: 40.r,
      decoration: BoxDecoration(
        color: md.isSelected ? ColorsUtil.theme.withAlpha(10) : ColorsUtil.bg,
        border: Border.all(
          color: md.isSelected ? ColorsUtil.theme : ColorsUtil.bg,
          width: 0.5,
        ),
      ),
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 10.r),
      child: Text(
        md.text,
        style: TextStyle(
          color: md.isSelected ? ColorsUtil.theme : ColorsUtil.content,
          fontSize: 12.sp,
        ),
      ),
    );
  }

  Widget _buildItemRadio({
    required SelectionModel md,
    Color? backgroundColor,
  }) {
    return Container(
      height: 50.r,
      alignment: Alignment.centerLeft,
      color: backgroundColor ??
          ((md.isShow || md.isSelected) ? Colors.white : ColorsUtil.bg),
      padding: EdgeInsets.symmetric(horizontal: 10.r),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Icon(
            md.isSelected
                ? Icons.check_box_rounded
                : Icons.check_box_outline_blank,
            color: md.isSelected ? ColorsUtil.theme : ColorsUtil.title,
            size: 18.r,
          ),
          SizedBox(
            width: 5.r,
          ),
          Text(
            md.text,
            style: TextStyle(
              color: md.isSelected ? ColorsUtil.theme : ColorsUtil.title,
              fontSize: 14.sp,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildItemCheckBox({
    required SelectionModel md,
    Color? backgroundColor,
  }) {
    var isChildrenSelected =
        md.list?.where((element) => element.isSelected).isNotEmpty == true;
    return Container(
      height: 50.r,
      alignment: Alignment.centerLeft,
      color: backgroundColor ?? (md.isShow ? Colors.white : ColorsUtil.bg),
      padding: EdgeInsets.symmetric(horizontal: 10.r),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Flexible(
            child: Text(
              md.text,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                color: ColorsUtil.title,
                fontSize: (md.isSelected || isChildrenSelected) ? 14.sp : 12.sp,
                fontWeight: (md.isSelected || isChildrenSelected)
                    ? FontWeight.bold
                    : FontWeight.normal,
              ),
            ),
          ),
          Icon(
            md.isSelected ? Icons.check_box : Icons.check_box_outline_blank,
            color: md.isSelected ? ColorsUtil.theme : const Color(0xffB9B9B9),
            size: 14.r,
          )
        ],
      ),
    );
  }

  Widget _buildItemTitle({
    required SelectionModel md,
  }) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          height: 50.r,
          color: Colors.white,
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.symmetric(horizontal: 12.r),
          child: Text(
            md.title ?? '',
            style: TextStyle(
              color: ColorsUtil.title,
              fontSize: 14.sp,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        _buildItemsView(md),
      ],
    );
  }

  Widget _buildItemDate({
    required SelectionModel md,
  }) {
    Widget itemView(String str) {
      return Container(
        width: 155.r,
        height: 40.r,
        decoration: BoxDecoration(
          color: ColorsUtil.bg,
          borderRadius: BorderRadius.circular(2),
        ),
        padding: EdgeInsets.symmetric(horizontal: 15.r),
        child: Row(
          children: [
            loadImage(url: 'date.png'.imgPathCommon, width: 14.r),
            SizedBox(
              width: 10.r,
            ),
            Text(
              str.isEmpty ? Ls.chooseDate : str,
              style: TextStyle(
                  color: str.isEmpty ? ColorsUtil.content : ColorsUtil.title,
                  fontSize: 15.sp),
            ),
          ],
        ),
      );
    }

    var ds = md.val.split(',');
    var begin = ds.first;
    var end = ds.last;
    return Container(
      height: 50.r,
      margin: EdgeInsets.symmetric(horizontal: 12.r),
      child: Row(
        children: [
          itemView(begin),
          Container(
            width: 30.r,
            alignment: Alignment.center,
            child: Text(
              '至',
              style: TextStyle(color: ColorsUtil.title, fontSize: 15.sp),
            ),
          ),
          itemView(end),
        ],
      ),
    );
  }

  Widget _buildItemInput({
    required SelectionModel md,
  }) {
    return Container(
      height: 40.r,
      decoration: const BoxDecoration(
        color: ColorsUtil.bg,
      ),
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 10.r),
      margin: EdgeInsets.symmetric(horizontal: 12.r),
      child: UITextField(
        hintText: Ls.searchKey,
        onChanged: (value) {},
      ),
    );
  }
}
